package com.xs.common.tkcore;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Iterator;
import java.util.Set;

/**
 * Created by huyapeng on 2018/5/9.
 */
public class UpdateListProvider extends MapperTemplate {

    public UpdateListProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    public String updateList(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\";\">");
        sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.updateSetColumns(entityClass, "item", true, this.isCheckExampleEntityClass()));

        Set<EntityColumn> columnSet = EntityHelper.getPKColumns(entityClass);
        Iterator var4 = columnSet.iterator();
        sql.append("<where>");
        while (var4.hasNext()) {
            EntityColumn column = (EntityColumn) var4.next();
            sql.append(" AND ").append(column.getColumn()).append(" = #{item.").append(column.getColumn()).append("}");
        }
        sql.append("</where>");
        sql.append("</foreach>");
        return sql.toString();
    }


}
